package com.wdy.golden.tinysql.util;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**  
 * @author Dongye_Wu
 * @version: 1.0.0
 * @Description: TODO
 * @date 2020年4月11日 下午10:26:51 
 */
public class StringUtil extends StringUtils {
	public static final char UNDERLINE = '_';
	public static final String METHOD_GET_PREFIX = "get";
	public static final String METHOD_SET_PREFIX = "set";
	
	public static boolean startsWithAnyAndIgnoreCase(CharSequence str, String[] prefixs) {
		return startsWithAnyAndIgnoreCase(str, Arrays.asList(prefixs));
	}
	
	public static boolean startsWithAnyAndIgnoreCase(CharSequence str, List<String> prefixs) {
		if(prefixs!=null && prefixs.size()>0) {
			for(String prefix : prefixs) {
				if(startsWithIgnoreCase(str, prefix)) {
					return true;
				}
			}
		}
		return false;
	}


	/**
	 * 驼峰格式字符串转换为下划线格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param) {		
		if (isEmpty(param.trim())) {
			return param;
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线格式字符串转换为驼峰格式字符串2
	 * 
	 * @param param
	 * @return
	 */
	public static String underlineToCamel2(String param) {		
		if (isEmpty(trim(param))) {
			return param;
		}
		StringBuilder sb = new StringBuilder(param);
		Matcher mc = Pattern.compile("_").matcher(param);
		int i = 0;
		while (mc.find()) {
			int position = mc.end() - (i++);
			sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
		}
		return sb.toString();
	}

	public static String underlineLowerToCamel(String param) {	
		return underlineToCamel2(lowerCase(param));
	}
	
	public static boolean underlineToCamelEquals(String underlineStr, String camelStr) {
		if(isEmpty(trim(underlineStr)) || isEmpty(trim(camelStr))) {
			return false;
		}
		if(camelStr.equalsIgnoreCase(underlineLowerToCamel(underlineStr))) {
			return true;
		}
		return false;
	}
	

	/**
	 * 下划线格式字符串转换为驼峰格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String underlineToCamel(String param) {		
		if (isEmpty(trim(param))) {
			return param;
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (c == UNDERLINE) {
				if (++i < len) {
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 将下划线字段转化为首字母大写的驼峰字段
	 * @param param
	 * @return
	 */
	public static String underlineToCapitalizeCamel(String param){
		return capitalize(underlineToCamel(param));
	}

	
	/**
	 * 将下划线字段转化为驼峰字段的get方法
	 * @param param
	 * @return
	 */
	public static String underlineToGetMethodName(String param){
		return METHOD_GET_PREFIX + underlineToCapitalizeCamel(param);		
	}
	
	/**
	 * 将下划线字段转化为驼峰字段的set方法
	 * @param param
	 * @return
	 */
	public static String underlineToSetMethodName(String param){
		return METHOD_SET_PREFIX + underlineToCapitalizeCamel(param);		
	}
	
    public static boolean notEquals(final CharSequence cs1, final CharSequence cs2) {
    	return !equals(cs1, cs2);
    }

	
    public static StringBuffer removeEnd(StringBuffer sb, String stripChars) {
    	if(sb!=null && sb.length()>0) {
    		String str = sb.toString();
    		if(endsWith(str, stripChars)) {
    			str = removeEnd(str, stripChars);
    			return new StringBuffer(str);
    		}
    	}
    	return sb;
    }
    
}
